#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include "lv_font_source_han_sans_bold.h" //中文字体的头文件

/**
 * 显示颜色标签和文本标签滑动
 */
void lv_bling_label_1(void)
{
    //静态的label标签
    lv_obj_t * label1 = lv_label_create(lv_scr_act());
    lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP);     /*Break the long lines*/
    lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_text(label1, "#0000ff Re-color# #ff00ff words# #ff0000 of a# label, align the lines to the center "
                              "and wrap long text automatically.");
    lv_obj_set_width(label1, 150);  /*Set smaller width to make the lines wrap*/
    lv_obj_set_style_text_align(label1, LV_TEXT_ALIGN_CENTER, 0);
    lv_obj_align(label1, LV_ALIGN_CENTER, 0, -40);

    //动态的label标签
    lv_obj_t * label2 = lv_label_create(lv_scr_act());
    lv_label_set_long_mode(label2, LV_LABEL_LONG_SCROLL_CIRCULAR);     /*Circular scroll*/
    lv_obj_set_width(label2, 150);
    lv_label_set_text(label2, "It is a circularly scrolling text. ");
    lv_obj_align(label2, LV_ALIGN_CENTER, 0, 40);
}

void lv_bling_label_2(void)
{
    //定义一个标签的对象
    lv_obj_t * label = lv_label_create(lv_scr_act());
    //设置文本内容
    // lv_label_set_text(label, "Hello, LVGL!");

    //使能字体的颜色
    lv_label_set_recolor(label, true); 
    lv_label_set_text(label,"#0000ff Hello, LVGL!#"); //设置文本颜色的格式
 
    //设置label的大小：
    lv_obj_set_width(label, 300); // 设置宽度为100px
    lv_obj_set_height(label, 50); // 设置高度为50px

    //设置字体的大小
    lv_obj_set_style_text_font(label, &lv_font_montserrat_20, 0);  
      
    //设置标签的位置
    lv_obj_set_pos(label,100,100);    
}    

static void event_handler_bnt_1(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    // int id = (int)lv_event_get_user_data(e);
    // printf("id=%d\n");

    if(code == LV_EVENT_PRESSED) {
        //按键发生LV_EVENT_PRESSED这个事件之后的处理如下
        // lv_obj_t * img2 = lv_img_create(lv_scr_act());
        // lv_obj_set_pos(img2,0,240);
        // lv_img_set_src(img2,  "S:/IOT/bling/200_200.jpg");
        // printf("show jpg200\n");
    
        printf("ok\n");
        // LV_LOG_USER("Clicked");
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        printf("no ok\n");
        // LV_LOG_USER("Toggled");
    }
}

//按键demo1
int lv_bling_btn_1()
{
     /*
        实现一个btn的功能 并在btn上面注释一个文本
    */
    //实例化一个按键btn的对象
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    //设置按键btn的size大小200*200
    lv_obj_set_size(btn1,200,200);
    //设置按键btn的位置(400,240)
    lv_obj_set_pos(btn1,400,240);
    //添加事件(自定义回调函数event_handler 函数句柄:最后一个参数NULL是传递给event_handler_bnt_1的参数)
    // lv_obj_add_event_cb(btn1, event_handler_bnt_1, LV_EVENT_PRESSED, (void *)1);    
    lv_obj_add_event_cb(btn1, event_handler_bnt_1, LV_EVENT_PRESSED, NULL);
    
    //显示文字在按键btn上
    lv_obj_t * label = lv_label_create(btn1);//实例化一个标签label对象
    lv_label_set_text(label, "Button bling");//给标签注释
    lv_obj_center(label); //将文本放在按键的中间  

    return 0;
}


//点击按键之后触发的事件
static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_CLICKED) {
        // LV_LOG_USER("bling-Clicked");
        printf("按键1\n");
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        // LV_LOG_USER("bling-Toggled");
        printf("按键2\n");
    }
}

void lv_bling_btn_2(void)
{
    lv_obj_t * label;

    // 创建一个按钮对象，父对象为当前活动屏幕（lv_scr_act()）
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    // 为按钮添加事件回调函数，监听所有事件（LV_EVENT_ALL）
    // 事件发生时将调用 event_handler 函数 (event_handler是一个回调函数)
    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
    // 将按钮居中显示，并向上偏移40像素（y坐标-40）
    // lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -40);
    lv_obj_align(btn1, LV_ALIGN_TOP_LEFT, 200, 100);
    
    // 在按钮上创建一个标签对象
    label = lv_label_create(btn1);
    // 设置标签的显示文本为"anjian1"（中文意思可能是"按键1"）
    lv_label_set_text(label, "anjian1");
    // 将标签在按钮内居中显示
    lv_obj_center(label);

    //按键2
    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn2, event_handler, LV_EVENT_ALL, NULL);
    // lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
    lv_obj_align(btn2, LV_ALIGN_TOP_RIGHT, -200, 100);
    lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE);
    lv_obj_set_height(btn2, LV_SIZE_CONTENT);

    label = lv_label_create(btn2);
    lv_label_set_text(label, "anjian2");
    lv_obj_center(label);

    //按键3
    lv_obj_t * btn3 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn3, event_handler, LV_EVENT_ALL, NULL);
    // lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
    lv_obj_align(btn3, LV_ALIGN_BOTTOM_LEFT, 200, -100);
    lv_obj_add_flag(btn3, LV_OBJ_FLAG_CHECKABLE);
    lv_obj_set_height(btn3, LV_SIZE_CONTENT);

    label = lv_label_create(btn3);
    lv_label_set_text(label, "anjian3");
    lv_obj_center(label);

    //按键4
    lv_obj_t * btn4 = lv_btn_create(lv_scr_act());
    lv_obj_add_event_cb(btn4, event_handler, LV_EVENT_ALL, NULL);
    // lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 40);
    lv_obj_align(btn4, LV_ALIGN_BOTTOM_RIGHT, -200, -100);
    lv_obj_add_flag(btn4, LV_OBJ_FLAG_CHECKABLE);
    lv_obj_set_height(btn4, LV_SIZE_CONTENT);

    label = lv_label_create(btn4);
    lv_label_set_text(label, "anjian5");
    lv_obj_center(label);    
}


/**
 * Show transformations (zoom and rotation) using a pivot point.
 * 显示官方的image图像效果
 */
static void set_angle(void * img, int32_t v)
{
    lv_img_set_angle(img, v);
}

static void set_zoom(void * img, int32_t v)
{
    lv_img_set_zoom(img, v);
}

void lv_bling_img_1(void)
{
    LV_IMG_DECLARE(img_cogwheel_argb);

    /*Now create the actual image*/
    lv_obj_t * img = lv_img_create(lv_scr_act());
    lv_img_set_src(img, &img_cogwheel_argb);
    lv_obj_align(img, LV_ALIGN_CENTER, 50, 50);
    lv_img_set_pivot(img, 0, 0);    /*Rotate around the top left corner*/

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, img);
    lv_anim_set_exec_cb(&a, set_angle);
    lv_anim_set_values(&a, 0, 3600);
    lv_anim_set_time(&a, 5000);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_start(&a);

    lv_anim_set_exec_cb(&a, set_zoom);
    lv_anim_set_values(&a, 128, 256);
    lv_anim_set_playback_time(&a, 3000);
    lv_anim_start(&a);
}


/*
    image图像
*/
void lv_bling_img_2(void)
{
    /*
        如何实现jpg图片
        显示jpg在坐标(0,0)的位置放了一张200_200.jpg图
    */
    //实例化一个img的对象
    lv_obj_t * img1 = lv_img_create(lv_scr_act());
    //将图片放在lcd(0,0)的坐标位置
    lv_obj_set_pos(img1,0,0);
    //设置图片的路劲(注意图片的路径要和开发板里面的路径一致)
    lv_img_set_src(img1,  "S:/IOT/bling/200.jpg");
    printf("show jpg200\n");
}




/*
    gif动态图
*/
void lv_bling_gif_1(void)
{
    #if 1
    /*
        如何实现gif动态图的控件
    */
    //显示gif在坐标(200,0)的位置放了一张gif图
    //实例化一个gif的对象
    lv_obj_t *gif1 = lv_gif_create(lv_scr_act());
    //设置gif图的坐标位置
    lv_obj_set_pos(gif1,200,0);
    //lv_obj_align(gif1, LV_ALIGN_RIGHT_MID, -20, 0); //在起始坐标的位置左移20
    //设置gif图的路径
    lv_gif_set_src(gif1,"S:/IOT/bling/1.gif");
    printf("show gif\n");
    #endif
}

/*
    消息对话框
*/
static void event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj));
}

void lv_bling_msgbox_1(void)
{
    static const char * btns[] ={"Apply", "Close", ""};

    lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "This is a message box with two buttons.", btns, true);
    lv_obj_add_event_cb(mbox1, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_center(mbox1);
}

/*
    列表框
*/
static lv_obj_t * list1;

static void event_list_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list1, obj));
    }
}

/*
    list列表控件
*/
void lv_bling_list_1(void)
{
    /*Create a list*/
    list1 = lv_list_create(lv_scr_act());
    lv_obj_set_size(list1, 180, 220);
    lv_obj_center(list1);

    /*Add buttons to the list*/
    lv_obj_t * btn;

    lv_list_add_text(list1, "File");
    btn = lv_list_add_btn(list1, LV_SYMBOL_FILE, "New");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_DIRECTORY, "Open");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_SAVE, "Save");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_CLOSE, "Delete");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_EDIT, "Edit");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list1, "Connectivity");
    btn = lv_list_add_btn(list1, LV_SYMBOL_BLUETOOTH, "Bluetooth");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_GPS, "Navigation");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_USB, "USB");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_BATTERY_FULL, "Battery");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);

    lv_list_add_text(list1, "Exit");
    btn = lv_list_add_btn(list1, LV_SYMBOL_OK, "Apply");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
    btn = lv_list_add_btn(list1, LV_SYMBOL_CLOSE, "Close");
    lv_obj_add_event_cb(btn, event_list_handler, LV_EVENT_CLICKED, NULL);
}


/*
中文测试
*/
static void event_font_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_PRESSED) {
        printf("ok\n");
    }

}

void font_test()
{
    /*
    显示中文字的说明:要添加 chinese_ziku.c lv_font_source_han_sans_bold_20.c两个文件	
    */
    //实例化一个按键btn的对象
    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn1,200,200);
    lv_obj_set_pos(btn1,400,240);
    lv_obj_add_event_cb(btn1, event_font_handler, LV_EVENT_PRESSED, NULL);

    lv_obj_t * wendu = lv_label_create(btn1);
    lv_obj_align(wendu,LV_ALIGN_CENTER,0,0);
    lv_label_set_text(wendu,"上一张");
    lv_obj_set_style_text_font(wendu,&lv_font_source_han_sans_bold_20,0); //20字体的大小
    // lv_obj_set_style_text_font(wendu,&chinese_ziku,0);//20字体的大小粗体


    //直接显示中文label标签
    lv_obj_t * label = lv_label_create(lv_scr_act());
    //设置文本内容
    lv_label_set_text(label, "中文好丝滑!");
    //设置label的大小：
    lv_obj_set_width(label, 300); // 设置宽度为100px
    lv_obj_set_height(label, 50); // 设置高度为50px        
    //设置中文字体的大小
    lv_obj_set_style_text_font(label,&lv_font_source_han_sans_bold_20,0);       
    //设置标签的位置
    lv_obj_set_pos(label,100,100);   
        
}

/*
    timer:定时器
*/
void my_timer(lv_timer_t * timer)
{
  /*Use the user_data*/
  uint32_t * user_data = timer->user_data;
  printf("my_timer called with user data: %d\n", *user_data);

  /*Do something with LVGL*/
//   if(something_happened) {
//     something_happened = false;
//     lv_btn_create(lv_scr_act(), NULL);
//   }
}

void timer_test()
{
    //定义一个定时器(每隔500ms执行一次my_timer的函数)
    static uint32_t user_data = 10;

    //user_data这个参数可以通过timer来传递给my_timer函数
    lv_timer_t * timer = lv_timer_create(my_timer, 500,  &user_data);

    //启动定时器
    // lv_timer_enable(true);

    lv_timer_ready(timer);

    //按键1 ：启动定时器
    //按键2：关闭定时器
}


//两个按键测试定时器
lv_timer_t * timer1 = NULL;
void my_timer_1(lv_timer_t * timer)
{
  /*Use the user_data*/
  uint32_t * user_data = timer->user_data;
  printf("my_timer called with user data: %d\n", *user_data);

}

static void event_handler_bnt_11(lv_event_t * e) //按键1启动定时器
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_PRESSED) {
        printf("ok\n");
        // lv_timer_ready(timer1);  //启动定时器
        lv_timer_resume(timer1); //恢复定时器
    }
}

static void event_handler_bnt_12(lv_event_t * e)//按键2暂停定时器
{
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_PRESSED) {
        printf("no ok\n");
        // lv_timer_del(timer1); //删除定时器
        lv_timer_pause(timer1); //暂停
    }
}

int timer_button_test()
{
    //初始化一个定时器(每500ms执行一个函数my_timer_1)
    static uint32_t user_data = 10;
    timer1 = lv_timer_create(my_timer_1, 500,  &user_data);

    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    //设置按键btn的size大小200*200
    lv_obj_set_size(btn1,100,100);
    //设置按键btn的位置(400,240)
    lv_obj_set_pos(btn1,600,100);   
    lv_obj_add_event_cb(btn1, event_handler_bnt_11, LV_EVENT_PRESSED, NULL);    
    //显示文字在按键btn上
    lv_obj_t * label1 = lv_label_create(btn1);//实例化一个标签label对象
    lv_label_set_text(label1, "开定时器");//给标签注释
    lv_obj_set_style_text_font(label1,&lv_font_source_han_sans_bold_20,0);    
    lv_obj_center(label1); //将文本放在按键的中间  

    lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
    //设置按键btn的size大小200*200
    lv_obj_set_size(btn2,100,100);
    //设置按键btn的位置(400,240)
    lv_obj_set_pos(btn2,600,300);   
    lv_obj_add_event_cb(btn2, event_handler_bnt_12, LV_EVENT_PRESSED, NULL);    
    //显示文字在按键btn上
    lv_obj_t * label2 = lv_label_create(btn2);//实例化一个标签label对象
    lv_label_set_text(label2, "关定时器");//给标签注释
    lv_obj_set_style_text_font(label2,&lv_font_source_han_sans_bold_20,0); 
    lv_obj_center(label2); //将文本放在按键的中间  

    return 0;
}
